package com.example.shop_2302.fg

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.viewpager2.widget.ViewPager2
import com.alibaba.android.arouter.launcher.ARouter
import com.example.base.entity.MyTabEntity
import com.example.base.mvvm.BaseFragment
import com.example.shop_2302.R
import com.example.shop_2302.adapter.VpAdapter
import com.example.shop_2302.databinding.FragmentMineBinding
import com.example.shop_2302.order.Order1Fragment
import com.example.shop_2302.order.Order2Fragment
import com.example.shop_2302.order.Order3Fragment
import com.example.shop_2302.userInfo.UserViewModel
import com.example.shop_2302.utils.GlideUtils
import com.flyco.tablayout.listener.CustomTabEntity
import com.flyco.tablayout.listener.OnTabSelectListener

class MineFragment : BaseFragment<FragmentMineBinding,UserViewModel>() {
    val fglist = mutableListOf<Fragment>()
    val tabs = arrayListOf<CustomTabEntity>()
    lateinit var vpAdapter: VpAdapter
    override fun initView() {
        //请求用户信息
        viewModel.user()

        fglist.add(Order1Fragment())
        fglist.add(Order2Fragment())
        fglist.add(Order3Fragment())
        vpAdapter = VpAdapter(requireActivity(),fglist)
        binding.vp.adapter = vpAdapter

        tabs.add(MyTabEntity("待付款",0,0))
        tabs.add(MyTabEntity("待发货",0,0))
        tabs.add(MyTabEntity("售后/退款",0,0))
        binding.common.setTabData(tabs)

        binding.vp.registerOnPageChangeCallback(object:ViewPager2.OnPageChangeCallback(){
            override fun onPageSelected(position: Int) {
                super.onPageSelected(position)
                binding.common.currentTab = position
            }
        })

        binding.common.setOnTabSelectListener(object:OnTabSelectListener{
            override fun onTabSelect(position: Int) {
                binding.vp.currentItem = position
            }

            override fun onTabReselect(position: Int) {

            }
        })

        //点击手机号，跳转到边界界面
        binding.userPhone.setOnClickListener {
            ARouter.getInstance().build("/app/EditActivity").navigation()
        }
    }

    override fun initData() {
        viewModel.userSuccess.observe(this){
            if(it.code==200){
                GlideUtils.loadCircle(requireActivity(),it.data.avatar,binding.userIv)
                binding.userName.text = it.data.nickname
                binding.userPhone.text = it.data.phone
            }
        }
    }

    override fun getViewModel(): Class<UserViewModel> = UserViewModel::class.java

    override fun getLayout(): Int = R.layout.fragment_mine

}